package com.tadpole.music.presenter.me;

import com.qiniu.util.Json;
import com.tadpole.music.bean.me.MoNiErrorBean;
import com.tadpole.music.bean.me.MoNiSearchBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.me.MoNiSearchIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 模拟筛选
 * Created By 孙岩
 * on 2020/12/27
 */
public class MoNiSearchPresenter extends BasePresenter<MoNiSearchIView> {
    /**
     *
     * @param user_id
     * @param rank_id
     * @param start_time
     * @param end_time
     * @param flag 1表示点击了左右按钮 2表示点击了当日 3表示导航
     */
    @SuppressWarnings("unchecked")
    public void getMoNiSearch(int user_id, int rank_id, String start_time, String end_time, final int flag) {
        if (isViewAttached()) {
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN, "");
            String url = UrlConfig.getMoNiSearch;
            DataModel.request(ModelToken.POST_HEAD_REQUEST)
                    .token(token)
                    .url(url)
                    .paramKey("user_id", "rank_id", "start_time", "end_time")
                    .paramValue(user_id + "", rank_id + "", start_time, end_time)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            try {
                                JSONObject obj = new JSONObject(data);
                                if (obj.getInt("code") == 200) {
                                    JSONObject object = new JSONObject(obj.getString("data"));
                                    List<MoNiErrorBean.DataBean.MonthClassStaticBean> monthClassList = new ArrayList<>();
                                    JSONArray monthClassArray = new JSONArray(object.getString("month_class_static"));
                                    for (int j = 0; j < monthClassArray.length(); j++) {
                                        JSONObject mothClassObj = new JSONObject(monthClassArray.get(j).toString());
                                        MoNiErrorBean.DataBean.MonthClassStaticBean monthClassStaticBean = new MoNiErrorBean.DataBean.MonthClassStaticBean(
                                                JsonParseUtil.getInt(mothClassObj, "class_id"),
                                                JsonParseUtil.getStr(mothClassObj, "name"),
                                                JsonParseUtil.getStr(mothClassObj, "rate"),
                                                JsonParseUtil.getInt(mothClassObj, "err_no")
                                        );
                                        monthClassList.add(monthClassStaticBean);
                                    }


                                    JSONObject mothDataObj = new JSONObject(object.getString("month_data"));
                                    List<Integer> daysList = new ArrayList<>();
                                    if (mothDataObj.isNull("days")) {
                                        daysList = null;
                                    } else {
                                        JSONArray daysArray = new JSONArray(mothDataObj.getString("days"));
                                        for (int a = 0; a < daysArray.length(); a++) {
                                            daysList.add(daysArray.getInt(a));
                                        }
                                    }
                                    MoNiErrorBean.DataBean.MonthDataBean monthDataBean = new MoNiErrorBean.DataBean.MonthDataBean(
                                            JsonParseUtil.getStr(mothDataObj, "month"),
                                            daysList
                                    );

                                    JSONObject monthDataObj = new JSONObject(object.getString("month_data_static"));
                                    MoNiErrorBean.DataBean.MonthDataStaticBean monthDataStaticBean = new MoNiErrorBean.DataBean.MonthDataStaticBean(
                                            JsonParseUtil.getStr(monthDataObj, "rate"),
                                            JsonParseUtil.getInt(monthDataObj, "total_err_no"),
                                            JsonParseUtil.getInt(monthDataObj, "total_questions"),
                                            JsonParseUtil.getInt(monthDataObj, "total_time")
                                    );

//                                    JSONObject totalObj = new JSONObject(JsonObject.getString("total"));
//                                    MoNiErrorBean.DataBean.TotalBean totalBean = new MoNiErrorBean.DataBean.TotalBean(
//                                            JsonParseUtil.getStr(totalObj,"rate"),
//                                            JsonParseUtil.getInt(totalObj,"total_err_no"),
//                                            JsonParseUtil.getInt(totalObj,"total_questions"),
//                                            JsonParseUtil.getInt(totalObj,"total_time")
//                                    );

                                    MoNiErrorBean.DataBean dataBean = new MoNiErrorBean.DataBean(
                                            monthDataBean,
                                            monthDataStaticBean,
//                                            JsonParseUtil.getStr(JsonObject,"name"),
                                            JsonParseUtil.getInt(object, "rank_id"),
//                                            totalBean,
                                            monthClassList
                                    );

                                    getView().showMoNiSearch(dataBean,flag);
                                } else if (obj.getInt("code") == 202) {
                                    ToastUtils.show("暂无答题数据");
                                } else {
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().showErr();
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}

